package com.qf.modules.sys.controller;

import com.qf.common.persistence.Page;
import com.qf.common.util.ResultInfo;
import com.qf.modules.sys.entity.User;
import com.qf.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public ResultInfo login(@RequestBody User user) {
        return userService.login(user);
    }

    @RequestMapping("register")
    public ResultInfo register(@RequestBody User user) {
        return userService.register(user);
    }

    @RequestMapping("getImageCode")
    public ResultInfo getImageCode(){
        return userService.getImageCode();
    }

    @RequestMapping("getListData")
    public ResultInfo getListData(HttpServletRequest request) {
        String account = request.getParameter("account");
        String loginName = request.getParameter("loginName");
        User user = new User();
        user.setAccount(account);
        user.setLoginName(loginName);
        List<User> userList = userService.findList(user);
        return ResultInfo.ok().setData(userList);
    }

    @RequestMapping("getPageData")
    public ResultInfo getPageData(User user, HttpServletRequest request){
        Page<User> page = userService.findPage(new Page<>(), user);
        return ResultInfo.ok().setData(page);
    }

    @RequestMapping("save")
    public ResultInfo save(@RequestBody User user){
        return userService.saveWithVerify(user);
    }

    @RequestMapping("permission")
    public ResultInfo permission() {
        String[] aa = {"sys:user:add", "sys:user:edit", "sys:user:delete", "sys:user:import", "sys:user:export"};
        return ResultInfo.ok().setData(aa);
    }
}
